<template>
	<view>
		<!--顶部S-->
		<view class="search_box level_align flex_between tp-bgf tp-ptb0lr30">
			<view class="search_v level_align tp-bgf">
				<view class="tp-mr20" @click="typeShow = !typeShow">
					{{searchType.name}}
				</view>
				<uni-icons @click="typeShow = !typeShow" type="arrowdown" size="16" color="#666"></uni-icons>
				<text class="line"></text>
				<image class="search-img tp-mr15" src="../../static/imgs/icons/search-icon.png" mode=""></image>
				<input class="flex-1" type="text" maxlength="6"  @input="kongge" v-model="searchVal" placeholder="请输入关键字" confirm-type="clickSearch" @confirm="clickSearch()">
				<view v-show="typeShow" class="type-box tp-br8">
					<view @click="selectType(item)" class="type-item tp-h64 flex align_center" v-for="(item,index) in typeList" :key="index">
						{{item.name}}
					</view>
				</view>
			</view>
			<view class="search center_combo tp-h88" @click="clickSearch">
				搜索
			</view>
		</view>
		<!--顶部E-->

		<view class="history_title_box level_align flex_between tp-h88">
			<view class="">
				历史记录
			</view>
			<view class="level_align">
				<view class=" tp-mr20">
					<image class="tp-img28" src="../static/icon/delete.png" mode=""></image>
				</view>
				<view class="" @click="delAll">
					清除历史
				</view>
			</view>
		</view>
		<view class="history_list flex flex_wrap ">
			<view class="tag tp-bgf tp-mb20 tp-mr20 tp-br8" @click="historyse(item)" v-for="(item,index) in HistoryList"
			 :key="index">
				<view class="level_align">
					<view class="ellispsis1">
						{{item}}
					</view>
					<view class="close-icon level_align" @click.stop="delOne(index)">
						<uni-icons type="closeempty" size="22" color="#999"></uni-icons>
					</view>
				</view>
			</view>
		</view>

		<view class="tp-fz26 tp-fc66 tp-w690 tp-mt20">
			热门推荐
		</view>
		<view class="history_list flex flex_wrap tp-mt20">
		<view class="tag tp-bgf tp-mb20 tp-mr20 tp-br8" @click="tuijian(item)" v-for="(item,index) in recommended"
		 :key="index">
			<view class="level_align">
				<view class="ellispsis1">
					{{item.name}}
				</view>
			</view>
		</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchVal: '', // 搜索的内容
				recommended: '郑州', // 当前推荐
				location: { //经纬度
					longitude: '',
					latitude: ''
				},
				typeShow: false,
				searchType: {//默认搜索类型
					name: '商品',
				},
				typeList: [ {//搜索类型
					name: '商品',
				},{
					name: '酒店',
				},{
					name: '景区',
					
				}],
				locationAddress: '', //详细地址
				HistoryList: [], // 搜索历史
			}
		},
		onLoad(option) {
			
		},
		onShow() {
			this.gettuijian()
			 this.HistoryList =  this.$store.getters.getHistorylist
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.gettuijian()
		},
		methods: {
			//去除input首尾空格
			kongge(e){
				this.searchVal = e.detail.value.trim()
			},
			// 点搜素
			clickSearch() {
				
				if(!this.searchVal){
					return uni.showToast({
						title: '请输入或选择搜索内容',
						icon: 'none'
					})
				}
				this.HistoryList.unshift(this.searchVal)//在数组头部添加
				for (let i = 0; i < this.HistoryList.length; i++) {//数组去重
					for (let j = i + 1; j < this.HistoryList.length; j++) {
						if (this.HistoryList[i] === this.HistoryList[j]) {
							this.HistoryList.splice(j, 1)
							j = j - 1;
						}
					}
				}
				 if(this.HistoryList.length > 10){//最多历史10条,多了就从最末尾删除
					this.HistoryList.pop()
				} 
				this.$store.commit('upHistorylist',this.HistoryList)//存到仓库
					uni.navigateTo({
						url: '/pagesSecond/strategy/strategyJoinitems?that='+ this.searchType.name + '&tit=' +  this.searchVal
					})
					this.searchVal = ''
			},
			// 选择搜索类型
			selectType(item) {
				this.searchType = item
				this.typeShow = !this.typeShow
			},
			
			// 删除单个记录
			delOne(index) {
				this.HistoryList.splice(index, 1)
				this.$store.commit('upHistorylist',this.HistoryList)
			},
			// 删除全部
			delAll() {
				this.HistoryList = []
				this.$store.commit('delHistorylist',this.HistoryList)
			},
			//选择历史调用搜索
			historyse(item){
				console.log(item)
				this.searchVal = item
				this.clickSearch()
			},
			//获取推荐列表
			gettuijian(){
				this.$http.post(this.$api.getHomeCarouselRecommend).then(res => {
					uni.stopPullDownRefresh();
					if(res.code == 200){
						this.recommended = res.data.result
					}
				})
			},
			// 选择地址
			tuijian(item){
				this.searchVal = item.name
				this.clickSearch()
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 顶部搜索
	.search_box {
		margin-bottom: 4rpx;
		background-color: #00B4FF;
	
		.search_v {
			position: relative;
			width: 619rpx;
			height: 56rpx;
			border-radius: 28rpx;
			padding-left: 30rpx;
		}
	
		.search {
			width: 70rpx;
			margin-left: 20rpx;
			font-size: 30rpx;
			color: #fff;
		}
	
		.line {
			height: 36rpx;
			width: 2rpx;
			background: #EAEAEA;
			margin: 0 20rpx;
		}
	
		.search-img {
			width: 28rpx;
			height: 28rpx;
		}
	
		.type-box {
			position: absolute;
			top: calc(100% + 2rpx);
			left: 20rpx;
			right: 0;
			width: 140rpx;
			background: #FFFFFF;
			color: #333333;
			z-index: 1;
	
			.type-item {
				border-left: 2rpx solid rgba(245, 245, 245, 1);
				border-right: 2rpx solid #F5F5F5;
				border-bottom: 2rpx solid #F5F5F5;
				padding-left: 20rpx;
			}
		}
	}
	
	.top-box {
		background: #00B4FF;
		position: fixed;
		/* #ifdef MP-WEIXIN */
		top: 0;
		/* #endif */
		/* #ifdef H5 */
		top: 88rpx;
		/* #endif */
		right: 0;
		z-index: 10000;
		width: 100%;

		/deep/ .uni-searchbar {
			height: 56rpx;
		}

		/deep/ .uni-searchbar__cancel {
			line-height: 56rpx;
		}

		/deep/ .uni-searchbar__cancel {
			color: #fff;
		}

	}


	.history_title_box {
		padding: 0 30rpx;
		font-size: 26rpx;
	}

	.history_list {
		padding: 0 40rpx;

		.tag {
			height: 60rpx;
			line-height: 60rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			border: 0;
			display: inline-block;
			font-family: PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 1);

			.close-icon {
				margin-left: 20rpx;
			}
		}
	}
</style>
